{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0fd9257-ae6c-41f3-835d-8aa396df59fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import pandas as pd\n",
    "\n",
    "# 定义两个网站的URL\n",
    "url1 = 'https://gl.ali213.net/html/2023-11/1224639.html'  # 第一个网站的URL\n",
    "url2 = 'https://www.shanghairanking.cn/rankings/bcur/2023'  # 第二个网站的URL\n",
    "\n",
    "\n",
    "# 定义函数来爬取一个网站的<a>标签，并返回链接和文本列表\n",
    "def crawl_links(url):\n",
    "    try:\n",
    "        # 发起请求获取网页内容\n",
    "        response = requests.get(url)\n",
    "        response.raise_for_status()  # 如果请求失败则抛出异常\n",
    "        response.encoding = 'utf-8'  # 设置正确的编码\n",
    "\n",
    "        # 使用BeautifulSoup解析网页内容\n",
    "        soup = BeautifulSoup(response.text, 'html.parser')\n",
    "        a_tags = soup.find_all('a')\n",
    "\n",
    "        # 提取链接和文本内容\n",
    "        links_and_texts = [(a_tag.get('href'), a_tag.get_text(strip=True)) for a_tag in a_tags]\n",
    "        return links_and_texts\n",
    "    except requests.RequestException as e:\n",
    "        print(f\"请求{url}时出错: {e}\")\n",
    "        return []\n",
    "\n",
    "    # 分别爬取两个网站的<a>标签\n",
    "\n",
    "\n",
    "links_and_texts_1 = crawl_links(url1)\n",
    "links_and_texts_2 = crawl_links(url2)\n",
    "\n",
    "# 创建DataFrame来存储链接和文本信息\n",
    "df1 = pd.DataFrame(links_and_texts_1, columns=['链接', '文本'])\n",
    "df2 = pd.DataFrame(links_and_texts_2, columns=['链接', '文本'])\n",
    "\n",
    "# 创建一个ExcelWriter对象，它将用于写入多个sheet\n",
    "with pd.ExcelWriter('links.xlsx', engine='openpyxl') as writer:\n",
    "    df1.to_excel(writer, sheet_name='Sheet1', index=False)\n",
    "    df2.to_excel(writer, sheet_name='Sheet2', index=False)\n",
    "\n",
    "print(\"链接信息已保存到Excel文件的两个sheet中。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "anaconda-panel-2023.05-py310",
   "language": "python",
   "name": "conda-env-anaconda-panel-2023.05-py310-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
